package com.dinree.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dinree.R;
import com.dinree.bean.SearchBean;
import com.dinree.utils.StringFormatUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuyinlei on 2017/3/31.
 *
 * @function 搜索adapter
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {


    private List<SearchBean> mDataBeen = new ArrayList<>();

    private StringFormatUtil mFormatUtil;

    private String mLightStr;

    /**
     * 添加数据
     *
     * @param dataBeen 集合
     * @param key      搜索key值
     */
    public void loadData(List<SearchBean> dataBeen, String key) {
        mDataBeen = dataBeen;
        mLightStr = key;
        notifyDataSetChanged();
    }

    /**
     * 上拉加载更多数据
     *
     * @param dataBeen 集合
     * @param key      搜索key值
     */
    public void loadMoreData(List<SearchBean> dataBeen, String key) {
        mDataBeen.addAll(dataBeen);
        mLightStr = key;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SearchBean dataBean = mDataBeen.get(position);

        mFormatUtil = new StringFormatUtil(holder.itemView.getContext(), dataBean.title, mLightStr, R.color.mainColor).fillColor();
        holder.tv_title.setText(mFormatUtil.getResult());
    }

    @Override
    public int getItemCount() {
        return mDataBeen == null ? 0 : mDataBeen.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;

        ViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }
}
